package com.hm.oper_monitor.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.hm.common.plugin.Page;
import com.hm.common.plugin.PageContext;
import com.hm.common.util.StackTraceUtil;

/**
 * 
 * Mybatis方式持久层公共常用数据库操作类
 * 
 * @author wujl
 * 
 */
@Repository
public class SqliteServiceUtilDao extends SqlSessionDaoSupport {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Resource(name = "sqlSessionFactory_sqlite")
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}

	protected String realSqlPath() {
		try {
			String oldSqlPath = this.getClass().getPackage().getName();
			if (oldSqlPath != null && !"".equals(oldSqlPath.trim())) {
				return oldSqlPath.replaceAll("dao", "mapper");
			} else {
				return "";
			}
		} catch (Exception ex) {
			logger.error(StackTraceUtil.getStackTrace(ex));
			return "";
		}

	}

	/**
	 * 根据条件查询记录
	 * 
	 * @param t
	 * @return
	 */
	@SuppressWarnings({ "unchecked" })
	public <T> List<T> selectList(String mapper, String statement, Object parameter, Class<T> clazz) {
		return (List<T>) getSqlSession().selectList(mapper + "." + statement, parameter);
	}

	protected <T> List<T> selectList(String statement, Object parameter, Class<T> clazz) {
		return selectList(realSqlPath(), statement, parameter, clazz);
	}

	/**
	 * 查询一条记录
	 * 
	 * @param t
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T selectOne(String mapper, String statement, Object parameter, Class<T> clazz) {
		return (T) getSqlSession().selectOne(mapper + "." + statement, parameter);
	}

	protected <T> T selectOne(String statement, Object parameter, Class<T> clazz) {
		return this.selectOne(realSqlPath(), statement, parameter, clazz);
	}

	/***
	 * 更新记录
	 * 
	 * @param mapper
	 * @param statement
	 * @param parameter
	 * @return
	 */
	public int update(String mapper, String statement, Object parameter) {
		return getSqlSession().update(mapper + "." + statement, parameter);
	}

	protected int update(String statement, Object parameter) {
		return this.update(realSqlPath(), statement, parameter);
	}

	/***
	 * 删除记录
	 * 
	 * @param mapper
	 * @param statement
	 * @param parameter
	 * @return
	 */
	public int delete(String mapper, String statement, Object parameter) {
		return getSqlSession().delete(mapper + "." + statement, parameter);
	}

	protected int delete(String statement, Object parameter) {
		return this.delete(realSqlPath(), statement, parameter);
	}

	/***
	 * 新增记录
	 * 
	 * @param mapper
	 * @param statement
	 * @param parameter
	 * @return
	 */
	public int insert(String mapper, String statement, Object parameter) {
		return getSqlSession().insert(mapper + "." + statement, parameter);
	}

	protected int insert(String statement, Object parameter) {
		return this.insert(realSqlPath(), statement, parameter);
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> selectListPage(String mapper, String statement, Object parameter, Class<T> clazz) {
		Page page = PageContext.getPage();
		RowBounds rowBounds = new RowBounds(page.getDisplayStart(), page.getDisplayLength());
		return (List<T>) this.getSqlSession().selectList(mapper + "." + statement, parameter, rowBounds);
	}

	protected <T> List<T> selectListPage(String statement, Object parameter, Class<T> clazz) {
		return this.selectListPage(realSqlPath(), statement, parameter, clazz);
	}

	// by hlm 2013-02-18 begin
	/***
	 * 调用存储过程新增记录
	 * 
	 * @param mapper
	 * @param statement
	 * @param params
	 * @return params
	 */
	public Map<String, Object> insertPro(String statement, Map<String, Object> params) {
		this.insert(realSqlPath(), statement, params);
		return params;
	}

	/***
	 * 调用存储过程修改记录
	 * 
	 * @param mapper
	 * @param statement
	 * @param params
	 * @return params
	 */
	public Map<String, Object> updatePro(String statement, Map<String, Object> params) {
		this.update(realSqlPath(), statement, params);
		return params;
	}

	/***
	 * 调用存储过程删除记录
	 * 
	 * @param mapper
	 * @param statement
	 * @param params
	 * @return params
	 */
	public Map<String, Object> deletePro(String statement, Map<String, Object> params) {
		this.delete(realSqlPath(), statement, params);
		return params;
	}

}
